{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>诊疗界面</title>
    <!-- 引入 layui.css -->
    <link href="{%static '/css/layui.css'%}" rel="stylesheet">
    <link href="{%static '/css/section.css'%}" rel="stylesheet">
    <link href="{%static '/css/font-awesome.min.css'%}" rel="stylesheet">
</head>
<body>

<!--诊疗工具栏-->

    <div class="layui-container">
        <div type="text-align:center;">
        <a href="javascript:;" class="layui-btn layui-btn-sm layui-btn-primary" data-content-href="zhenliao?type=self" data-title="自我诊断" data-icon="fa fa-gears">自我诊断</a>
        <a href="javascript:;" class="layui-btn layui-btn-sm" data-content-href="zhenliao?type=major" data-title="专业诊断" data-icon="fa fa-gears">专业诊断</a>
        </div>
<!--        以列表的形式显示云医的界面-->
        {%if all %}
        <script id="zhenliaoBar" type="text/html">
            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary" id="enter_btn" lay-event="enter_event">
                诊疗
            </button>
        </script>
        <table class="layui-table" style="height:full-150" id="infoTable"></table>
        {% endif %}

        {%if self %}
            <!--        自我诊断的界面-->
            <script id="drugBar" type="text/html">
                <button type="button" class="layui-btn layui-btn-sm" id="assignbtn" lay-event="edit_event">
                    编辑
                </button>
                <button type="button" class="layui-btn layui-btn-sm" id="cancel_btn" lay-event="del_event">
                    删除
                </button>
            </script>
            <div class="layui-form">
                <input type="hidden" id="prescript_id" value="{{prescription_id}}" />
                <input type="hidden" id="drug_id" />
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">订单编号</label>
                        <div class="layui-input-inline layui-input-wrap">
                            <input type="text" name="OrderNo" id="OrderNo" value="{{OrderNum}}" lay-verify="required|phone" autocomplete="off" lay-reqtext="订单编号" class="layui-input layui-disabled" disabled/>
                        </div>
                        <label class="layui-form-label">开药日期</label>
                        <div class="layui-input-inline layui-input-wrap">
                            <input type="text" name="date" id="date1" autocomplete="off" lay-reqtext="开药日期" value="{{OrderDt}}" class="layui-input layui-disabled" disabled>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">药品</label>
                        <div class="layui-input-inline layui-input-wrap">
                            <select id="tablet_info" name="tablet_info" lay-verify="required" lay-reqtext="药品必填" lay-search>
                                <option value="">请选择药品</option>
                                {% for tablet_obj in tablet_info_list%}
                                <option value="{{tablet_obj.id}}">{{tablet_obj.tblt_nm}}</option>
                                {% endfor %}
                            </select>
                        </div>
                        <label class="layui-form-label">药品备注</label>
                        <div class="layui-input-inline layui-input-wrap">
                            <input type="text" name="MedNote" id="MedNote" autocomplete="off" lay-reqtext="请填写备注" lay-affix="clear" class="layui-input">
                        </div>
                        <label class="layui-form-label">药品规格</label>
                        <div class="layui-input-inline layui-input-wrap">
                            <input type="text" name="MedSpec" id="MedSpec" autocomplete="off" lay-reqtext="请填写药品规格" lay-affix="clear" class="layui-input">
                        </div>
                    </div>
                  </div>
                  <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">服药备注</label>
                        <div class="layui-input-inline layui-input-wrap">
                            <select id="drtscd" name="drtscd" lay-verify="required" lay-search>
                                <option value="">请选择</option>
                                <option value="饭前">饭前</option>
                                <option value="饭后">饭后</option>
                            </select>
                        </div>
                        <label class="layui-form-label">服药时间</label>
                        <div class="layui-input-inline layui-input-wrap">
                            <select id="doselist_time" name="doselist_time" lay-verify="required" lay-search>
                                <option value="">请选择</option>
                                <option value="0800">早</option>
                                <option value="1200">中</option>
                                <option value="1800">晚</option>
                                <option value="0800,1800">早晚</option>
                                <option value="0800,1200,1800">早中晚</option>
                            </select>

                        </div>
                        <label class="layui-form-label">服药剂量</label>
                        <div class="layui-input-inline layui-input-wrap">
                            <input type="number" name="doselist_dose" id="doselist_dose" autocomplete="off" lay-reqtext="请填写服药剂量" class="layui-input">
                        </div>
                    </div>
                  </div>
            </div>
            <button type="button" id="btn_save" class="layui-btn layui-btn-primary">保存</button>&nbsp;&nbsp;&nbsp;
            <button type="button" id="btn_cancel" class="layui-btn layui-btn-primary">取消</button>
        <table class="layui-table" style="height:full-150" id="infoDrug"></table>
        {% endif %}

        {%if zhuan %}
        <!--        以图表的形式显示云医-->
            <section class="view-content content">
                {% for depart_type in depart_type_list%}
                <div class="section-block">
                    <div class="section-block-name">{{depart_type.name}}</div>
                    <div class="section-block-area">
                    {% for department in depart_type.department_set.all %}
                        <a onclick="gotohospital('{{department.name}}','{{department.url}}')" href="javascript:;" class="section-block-area-item" rel="">
                            <img class="section-block-area-item-img" src="{%static '/img/'%}{{department.img}}" />
                            <div class="section-block-area-item-name">{{department.name}}</div>
                        </a>
                    {% endfor %}
                    </div>
                </div>
                {% endfor %}
            </section>
        {% endif %}
        <br/>
        <br/>
    </div>

<!-- 引入 layui.js -->
<script src="{%static '/js/layui.min.js'%}" charset="utf-8"></script>
<script>

    layui.use(['layer','jquery','table','laydate','form'], function () {
        var $ = layui.jquery,
            layer = layui.layer,
            table = layui.table,
            laydate = layui.laydate,
            form = layui.form;

        window.gotohospital = function(title,url) {
            layer.open({
                title:"友情赞助商:"+title,
                maxmin:true,
                area:['78%','86%'],
                type: 2,
                content: [url,'yes']
            });
        }

        if("{{self}}"=="True"){
            laydate.render({
            elem:"#date1"
            });
            $("#btn_cancel").click(function(){
                layer.confirm('确认取消该处方？', {icon: 3,title:'取消'}, function(){
                    $.ajax({
                       url:"/api/prescription/prescription/{{prescription_id}}/",
                       type:"PUT",
                       data:{"is_Active":0},
                       dataType:"json",
                       success:function(data){
                           if(data.code="200"){
                               layer.msg(data.msg,{time:1*1000},function(){
                                               window.location="/index"
                                           });
                           }
                       }
                   });
                  }, function(){
                  });

            });
            $("#btn_save").click(function(){
               var tablet_options = $("#tablet_info").val()
               msg = "请选择:"
               if(tablet_options=="")
               {
                   msg += "药品\r\n"
               }
               var drtscd_options = $("#drtscd").val()
               if(drtscd_options=="")
               {
                   msg += "服药备注\r\n"
               }
               var doselist_time_options = $("#doselist_time").val()
               if(doselist_time_options=="")
               {
                   msg += "服药时间"
               }

               if (msg.trim().length > 0){
                   layer.msg(msg);
                   return;
               }



               drgu_id = $("#drug_id").val()
               if(drgu_id>0){
                   $.ajax({
                       url:"/api/tablet/tabletinfo/?id="+tablet_options,
                       type:"GET",
                       dataType:"json",
                       success:function(data){
                           if(data.code="200"){
                               tabletinfo = data.data[0]

                               $.ajax({
                                   url:"/api/prescription/drug/"+drgu_id+"/",
                                   type:"put",
                                   dataType:"json",
                                   data:{
                                       "tablet":tablet_options,
                                       "MedCd":tabletinfo.tblt_cd,
                                       "MedUnit":tabletinfo.tblt_unit,
                                       "MedNm":tabletinfo.tblt_nm,
                                       "MedSpec":$("#MedSpec").val(),
                                       "MedNote":$("#MedNote").val(),
                                       "prescrption":"{{prescription_id}}",
                                       "drtscd":$("#drtscd").val(),
                                       "takedays":1,
                                       "doselist_time":$("#doselist_time").val(),
                                       "doselist_dose":$("#doselist_dose").val()
                                   },
                                   success:function(data){
                                       if(data.code="200"){
                                           $("#drug_id").val("");
                                           $("#tablet_info").val("");
                                           $("#MedNote").val("");
                                           $("#MedSpec").val("");
                                           $("#drtscd").val("");
                                           $("#doselist_time").val("");
                                           $("#doselist_dose").val("");
                                           form.render('select')
                                           layer.msg(data.msg,{time:1*1000},function(){
                                               table.reload('infoDrug',function(){
                                                   page:{curr:1}
                                               });
                                           });
                                       }
                                   }
                               });
                           }
                       }
                   });
               }
               else
               {
                   $.ajax({
                       url:"/api/tablet/tabletinfo/?id="+tablet_options,
                       type:"GET",
                       dataType:"json",
                       success:function(data){
                           if(data.code="200"){
                               tabletinfo = data.data[0]
                               if ({{prescription_id}}>0)
                               {
                                   $.ajax({
                                       url:"/api/prescription/drug/",
                                       type:"POST",
                                       dataType:"json",
                                       data:{
                                           "tablet":tablet_options,
                                           "MedCd":tabletinfo.tblt_cd,
                                           "MedUnit":tabletinfo.tblt_unit,
                                           "MedNm":tabletinfo.tblt_nm,
                                           "MedSpec":$("#MedSpec").val(),
                                           "MedNote":$("#MedNote").val(),
                                           "prescrption":"{{prescription_id}}",
                                           "drtscd":$("#drtscd").val(),
                                           "takedays":1,
                                           "doselist_time":$("#doselist_time").val(),
                                           "doselist_dose":$("#doselist_dose").val()
                                       },
                                       success:function(data){
                                           if(data.code="200"){
                                               $("#drug_id").val("");
                                               $("#tablet_info").val("");
                                               $("#MedNote").val("");
                                               $("#MedSpec").val("");
                                               $("#drtscd").val("");
                                               $("#MedSpec").val("");
                                               $("#drtscd").val("");
                                               $("#doselist_time").val("");
                                               $("#doselist_dose").val("");
                                               form.render('select');
                                               table.reload('infoDrug',function(){
                                                   page:{curr:1}
                                               });
                                           }
                                       }
                                   });
                               }
                               else{

                                   layer.msg("{{OrderDt}}")
                                   $.ajax({
                                       url:"/api/prescription/prescription/",
                                       type:"POST",
                                       dataType:"json",
                                       data:{
                                           "OrderDt":"{{OrderDt}}",
                                           "OrderNum":"{{OrderNum}}",
                                           "MedItem":[
                                               {
                                                   "MedCd":tabletinfo.tblt_cd,
                                                   "MedUnit":tabletinfo.tblt_unit,
                                                   "MedNm":tabletinfo.tblt_nm,
                                                   "MedSpec":$("#MedSpec").val(),
                                                   "MedNote":$("#MedNote").val(),
                                                   "drtscd":$("#drtscd").val(),
                                                   "takedays":1,
                                                   "doselist_time":$("#doselist_time").val(),
                                                   "doselist_dose":$("#doselist_dose").val()
                                               }
                                           ]
                                       },
                                       success:function(data){
                                           if(data.code="200"){

                                           }
                                       }
                                   });
                               }
                           }
                       }
                   });
               }
            });
            table.render({
               elem:"#infoDrug",
               url:"/api/prescription/drug?prescrption_id={{prescription_id}}",
               height:'full-215',
               page:true,
               cols:[[
                   {toolbar:'#drugBar',title:"操作",width:140,fixed:'right'},
                   {title:'序号',width:70,type:"numbers"},
                   {field:'MedCd',title:"药品编号" },
                   {field:'MedNm',title:"药品名称" },
                   {field:'MedSpec',title:"药品规格" },
                   {field:'MedNote',title:"药品备注" },
                   {field:'drtscd',title:"服药备注" },
                   {field:'doselist_time',title:"服药时间" },
                   {field:'doselist_dose',title:"服药剂量" },
               ]],
               //修改参数
               request:{
                   "pageName":"pageNum",
                   "limitName":"pageSize"
               },
               response:{
                   statusName:'code',     //数据状态的字段名称，默认：codes
                   countName: 'count',    //数据总数的字段名称，默认：count
                   dataName:'data',       //数据列表的字段名称，默认：data
               },
               parseData:function(res){
                   d = {
                       "code":0,
                       "count":res.data.count,
                       "data":res.data.results
                   }
                   return d
               }
            });
            table.on('tool(infoDrug)',function(obj){

               if(obj.event == 'edit_event'){
                   drug_obj = obj.data;
                   $("#drug_id").val(drug_obj.id);
                   $("#tablet_info").val(drug_obj.tablet);
                   $("#MedNote").val(drug_obj.MedNote);
                   $("#drtscd").val(drug_obj.drtscd);
                   $("#doselist_time").val(drug_obj.doselist_time);
                   $("#doselist_dose").val(drug_obj.doselist_dose);
                   form.render('select')
               }
               else if(obj.event == 'del_event'){
                   data = obj.data;
                   url = "/api/prescription/drug/"+data.id+"/"
                   $.ajax({
                       url:url,
                       type:'delete',
                       dataType:"json",
                       success:function(data){
                           if(data){
                               layer.msg(data.msg,{time:1*1000},function(){
                                   table.reload('infoDrug',function(){
                                       page:{curr:1}
                                   });
                               });
                           }
                           else{
                               layer.msg("删除成功",{time:1*1000},function(){
                                   table.reload('infoDrug',function(){
                                       page:{curr:1}
                                   });
                               });
                           }
                       }
                   })
               }
           });
        }

        if("{{all}}"=="True"){
            table.render({
               elem:"#infoTable",
               url:"/api/cloud/hospital/",
               height:'full-215',
               page:true,
               cols:[[
                   {toolbar:'#zhenliaoBar',title:"操作",width:100,fixed:'left'},
                   {title:'序号',width:70,type:"numbers"},
                   {field:'name',title:"诊疗机构" },

               ]],
               //修改参数
               request:{
                   "pageName":"pageNum",
                   "limitName":"pageSize"
               },
               response:{
                   statusName:'code',     //数据状态的字段名称，默认：codes
                   countName: 'count',    //数据总数的字段名称，默认：count
                   dataName:'data',       //数据列表的字段名称，默认：data
               },
               parseData:function(res){
                   d = {
                       "code":0,
                       "count":res.data.count,
                       "data":res.data.results
                   }
                   return d
               }
            });
            table.on('tool(infoTable)',function(obj){
               if(obj.event == 'enter_event'){
                   hospital_obj = obj.data;
                   url = hospital_obj.url_address;
                   title = hospital_obj.name;
                   gotohospital(title,url);
               }
            });
        }
    });
</script>
</body>
</html>